package com.xiaoma.controller;

import com.xiaoma.pojo.Result;
import com.xiaoma.pojo.User;
import com.xiaoma.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api")
public class UserController{
    @Autowired
    private UserService userService;

    /**
     *添加用户
     */
    @PostMapping("addUser")
    public Result<User> addUser(@RequestBody User user){
        userService.addUser(user);
        //log.info("添加用户成功:{}", user);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("deleteUser")
    public Result<User> deleteUser(Integer id){
        userService.deleteUser(id);
        //log.info("删除用户成功:{}", id);
        return Result.success();
    }

    /**
     * 回显单个用户
     */
    @GetMapping("getUserById/{id}")
    public Result<User> getUserById(@PathVariable("id") Integer id){
        User user = userService.getUserById(id);
        //log.info("回显用户成功:{}", user);
        return Result.success(user);
    }

    /**
     * 修改用户
     */
    @PutMapping("updateUser")
    public Result<User> updateUser(@RequestBody User user){
        userService.updateUser(user);
        //log.info("修改用户成功:{}", user);
        return Result.success();
    }

    /**
     * 获取用户列表
     */
    @GetMapping("getUserList")
    public Result<List<User>> getUserList(){
        List<User> userList = userService.getUserList();
        //log.info("获取用户列表成功:{}", userList);
        return Result.success(userList);
    }
}
